< previous page page_592 next page >

Page 592
e10a6a0a59f035e870d90cca05b85fce.gif
    A = Vx * Vx + Vy * Vy + Vz * Vz
    B = 2 * Vx * (px - Cx) + _
        2 * Vy * (py - Cy) + _
        2 * Vz * (pz - Cz)
    C = Cx * Cx + Cy * Cy + Cz * Cz + _
        px * px + py * py + pz * pz - _
        2 * (Cx * px + Cy * py + Cz * pz) - _
        Radius * Radius

    ' Solve the quadratic A*t^2 + B*t + C = 0.
    B24AC = B * B - 4 * A * C
    If B24AC < 0 Then
        ' There is no real intersection.
        RayTraceable_FindT = -1
    ElseIf B24AC = 0 Then
        ' There is one intersection.
        T1 = -B / 2 / A
    Else
        B24AC = Sqr(B24AC)
        T1 = (-B + B24AC) / 2 / A
        T2 = (-B - B24AC) / 2 / A
        ' Use only positive t values.
        If T1 < 0.01 Then T1 = T2
        If T2 < 0.01 Then T2 = T1
        ' Use the smaller t value.
        If T1 > T2 Then T1 = T2
    End If

    ' If there is no positive t value, there's no
    ' intersection in this direction.
    If T1 < 0.01 Then
        RayTraceable_FindT = -1
    Else
        RayTraceable_FindT = T1 
    End If
End Function
Calculating Hit Color
Once the ray-tracing algorithm has determined which object is closest to the center of projection, it must determine the color of the object at that point. You can use the techniques explained in Chapter 16, Shading Models, to calculate the color.
The RayTraceable class defines the FindHitColor subroutine. This routine takes as parameters the coordinates of the point of interest. It returns the point's red, green, and blue color components through parameters passed by reference.
The following code shows how the RaySphere class calculates hit color. It calculates the sphere's unit normal vector at the point and then calls the CalculateHitColor sub-

 
< previous page page_592 next page >